package com.example.hibernatevalidatebean.controller;

import com.example.hibernatevalidatebean.api.IXJValidate;
import com.example.hibernatevalidatebean.entity.Employee;
import com.example.hibernatevalidatebean.entity.User;
import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.tags.BindErrorsTag;

import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Iterator;
import java.util.Set;

/**
 * @version 1.0
 * @program: hibernate-validate-bean
 * @packageName: com.example.hibernatevalidatebean.controller
 * @className HelloController
 * @description: TODO
 * @author: xj
 * @create: 2021-01-18 15:58:17
 **/
@RestController
public class HelloController {

    /*
     * 第一种方法
     * */
    @RequestMapping(value = "hello",method = RequestMethod.GET)
    public String hello(@RequestBody @Valid Employee employee, BindingResult bindingResult){
        System.out.println(employee);
        System.out.println(bindingResult);
        if (bindingResult.hasErrors()) {
            for (ObjectError error : bindingResult.getAllErrors()) {
                System.out.println(error.getDefaultMessage());
            }
            return "fail";
        }else{
            return "hello";
        }
    }

    /**
     * 第二种方法更通用
     * 核心依赖：封装到springboot
     * <dependency>
     *             <groupId>org.springframework.boot</groupId>
     *             <artifactId>spring-boot-starter-validation</artifactId>
     * </dependency>
     *
     * 有多个不满足,只提示第一个异常的情况
     **/
    public static void main(String[] args) {
        User user=new User();

        Validator validator = ((HibernateValidatorConfiguration) Validation.byProvider(HibernateValidator.class).configure()).failFast(true).buildValidatorFactory().getValidator();

        Set<ConstraintViolation<User>> violationSet = validator.validate(user, IXJValidate.class);
        boolean hasError = violationSet != null && violationSet.size() > 0;
        if (hasError) {
            Iterator var5 = violationSet.iterator();

            while(var5.hasNext()) {
                ConstraintViolation<User> violation = (ConstraintViolation)var5.next();
                System.out.println("校验结果1:"+violation.getPropertyPath().toString());
                System.out.println("校验结果2:"+violation.getMessageTemplate().toString());
                System.out.println();
            }
        }else{
            System.out.println("校验成功");
        }
    }

}
